//
//  FirstView.m
//  SweetComics
//
//  Created by LeeZhe on 15/1/29.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "FirstView.h"
#import "AFHTTPRequestOperationManager.h"
#import "MBProgressHUD.h"
#import "DetailViewController.h"
#import "UIImageView+WebCache.h"

@implementation FirstView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)createView
{
    
    
    //    UICollectionViewFlowLayout * flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    // 每个项的大小
    //    flowLayOut.itemSize = CGSizeMake(90,400);
    
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width + 10, height)
                                             collectionViewLayout:[[CircleLayout alloc] init]];
    [self.collectionView registerClass:[ShowImageCell class] forCellWithReuseIdentifier:KCellIdentifier];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    //    self.collectionView.pagingEnabled = YES;
    [self.collectionView setContentOffset:CGPointMake(width, 0.0F)];
    self.collectionView.dataSource = self;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100, 50)];
    label.text = @"甜心漫画";
    label.textColor = [UIColor orangeColor];
    label.font = [UIFont systemFontOfSize:20];
    self.backView = [[UIImageView alloc] initWithFrame:CGRectMake(-100,0, self.frame.size.width, self.frame.size.height)];
    _backView.image = [UIImage imageNamed:@"首页背景.jpg"];
//    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"HappyLife.jpg"]];
    self.backgroundColor = [UIColor whiteColor];
//    _collectionView.backgroundView = _backView;
//    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"首页背景"]];
//    view.frame = self.frame;
    _collectionView.backgroundView = _backView;
//    self.backgroundColor = [UIColor colorWithPatternImage:_backView.image];
    [self addSubview:_collectionView];
    [self addSubview:label];
    [self conectNetWork];

    [label release];
    
}

- (void)conectNetWork
{
    self.arr = [NSMutableArray array];
    __block  FirstView *homePage = self;
    // AFNetWorking
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kHotURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        for (NSMutableDictionary *comicDic in arr) {
            ComicList *commics = [[ComicList alloc] init];
            [commics setValuesForKeysWithDictionary:comicDic];
            [homePage.arr addObject:commics];
            
        }
        [homePage.collectionView reloadData];



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}





- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // must be dequeueReusableCellWithReuseIdentifier !!!!
    ShowImageCell *cell = (ShowImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:KCellIdentifier
                                                                            forIndexPath:indexPath];
    if (!cell) {
        NSLog(@"what ? cell is nil ? It's joke !");
        return nil;
    }
    ComicList *comics = [self.arr objectAtIndex:indexPath.item];
    [cell setComicsCell:comics];
//    NSLog(@"%ld  %@",indexPath.item,comics.title);

    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    ComicList *comics = [self.arr objectAtIndex:indexPath.item];
//    NSLog(@"~~~~~~   %ld %@",indexPath.row,comics.title);
    //    NSLog(@"%ld",indexPath.row);
    [self.delegate pushToDetailControll:self.arr];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    //无限循环....
    float targetX = _scrollView.contentOffset.x;
    NSInteger numCount = [self.collectionView numberOfItemsInSection:0];
    float ITEM_WIDTH = _scrollView.frame.size.width;
    
    if (numCount>=3)
    {
        if (targetX < ITEM_WIDTH/2) {
            [_scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
        }
        else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
        {
            [_scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
        }
    }
    
}
- (void)dealloc
{
    [_backView release];
    [_rightButton release];
    [_cell release];
    [_collectionView release];
    [super dealloc];
}


@end
